package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.ManholeCurDao;
import cn.topevery.garden.entity.po.ManholeCur;
import cn.topevery.garden.entity.query.ManholeCurQuery;
import cn.topevery.garden.entity.vo.ManholeCurVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 井盖当前状态表
 *
 * @author code-generate
 * @date 2021-09-06
 */
@Repository
public class ManholeCurDaoImpl extends BaseDaoImpl<ManholeCur> implements ManholeCurDao {
    @Override
    public <P extends DbCsp> List<ManholeCur> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<ManholeCurVO> listPage(ManholeCurQuery query) {
        String table = "t_manhole_cur tmc";

        String columns = "tmc.*";
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tmc.c_db_last_update_date desc");
        sqlQueryMate
                .where(true, "tmc.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getCode()), "tmc.c_device_code", ScriptConditionEnum.Like, query.getCode());

        return getDataPager(sqlQueryMate, pageSize, pageIndex, ManholeCurVO.class);
    }

    @Override
    public ManholeCur getByDeviceCode(String deviceCode) {
        String table = "t_manhole_cur tmc";

        String columns = "tmc.*";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tmc.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tmc.c_device_code", ScriptConditionEnum.Eq, deviceCode);
        AtomicReference<ManholeCur> manholeCur = new AtomicReference<>(new ManholeCur());
        run(sqlQueryMate.getSql(), CommandType.Text, dbCommand -> {
            sqlQueryMate.getParameters().forEach(dbCommand::addParameter);
            manholeCur.set(dbCommand.executeToEntity(ManholeCur.class));
        });
        return manholeCur.get();
    }
}
